var express = require('express');
var router = express.Router();
var CONFIG = require('../../../common/config');
var CODE = require('../../../common/code');
var superagent = require('superagent');
var sundryUtil = require('./util/sundryUtil');
var retUtil = require('./util/retUtil');
var StaticizeUtil = require('../../../common/util/StaticizeUtil');




router.get('/',function (req, res, next) {
    res.send('respond with a sundry resource');
});


/***
 * 获取首页信息_APP_6_4 添加
 * @author 毛凯
 * @date 2018-4-17
 * 方式: GET
 * 参数: device   设备的标识  610
 * 参数: appVer  版本号
 *测试地址 : http://localhost:3000/yjxc/yjscPlatform/design/v1/users/getAppHomePageInfo_app_v6_4?device=610&appVer=6.4
 */
function getAppHomePageInfo_app_v6_4(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');
    appVer = parseFloat(appVer);
    if (!device || !appVer) {
        return retUtil.ret_err_json("参数有误", res);
    }
    // 优先使用缓存
    var uri = "yjsc/v2/sundry/getAppHomePageInfo_app_v6_4";
    var para = {
        device : device , // 每种来源记录一份缓存
        appVer : appVer
    };
    StaticizeUtil.getByRedis(__filename, uri, para, function(error, content) {
        if (content && CONFIG.DEBUG !== 1) { // 有缓存, 且不是本地调试, 直接返回
            return retUtil.ret_succ_json(content, res);
        }
        sundryUtil.getAppHomePageInfo_app_v6_4(device,appVer, function (err, data) {
            if (err) {
                return retUtil.ret_err_json(err, res);
            }
            var content = StaticizeUtil.recordByRedis(__filename, uri, para, data);  // 缓存
            return retUtil.ret_succ_json(content, res);
        });
    });
}
router.post('/getAppHomePageInfo_app_v6_4', getAppHomePageInfo_app_v6_4);
router.get('/getAppHomePageInfo_app_v6_4', getAppHomePageInfo_app_v6_4);



/***
 * 模板/商品搜索_APP_6_6 添加
 * @author 李强
 * @date 2018-5-15
 * 方式: GET
 * 参数: device             设备的标识  610
 * 参数: appVer             版本号
 * 参数: pageNum            页码
 * 参数: pageSize           页容量
 * 参数: keyword            搜索参数, 长度 0-50
 * 参数: typeLs             模板一级分类, 数组的 json 字符串, 不传标记不限
 * 参数: vips               vip 类型, 数组的 json 字符串, 0 标记免费, 10 标记vip
 * 参数: orderBy            0:先按 order 排序, 再按 update_time 排序(默认)/1:按创建时间排序/2:按热度/使用次数排序
 * 参数: withTotalPage      是否获取总页数, 默认不获取
 *测试地址 : http://localhost:3000/yjsc/v2/sundry/searchDesignAndPrint_app_v6_6?
 */
function searchDesignAndPrint_app_v6_6(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');
    var pageNum = retUtil.numberFromReq(req, 'pageNum');
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 12;
    var keyword = retUtil.stringFromReq(req, 'keyword') || "";
    var typeLs = retUtil.arrayFromReq(req, 'typeLs') || [1, 4, 6, 7, 8];
    var vips = retUtil.arrayFromReq(req, 'vips') || [0,10];
    var orderBy = retUtil.numberFromReq(req, 'orderBy');
    var withTotalPage = retUtil.numberFromReq(req, 'withTotalPage');
    appVer = parseFloat(appVer);
    if (!device || !appVer ) {
        return retUtil.ret_err_json("参数有误", res);
    }
    if(appVer<6.6){
        return retUtil.ret_err_json("当前版本不支持", res);
    }

    //过滤参数中的特殊字符
    var pattern = new RegExp("[`~!@#$^&*%()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
    var keywordRs = "";
    for(var i = 0; i < keyword.length; i++) {
        keywordRs += keyword.substr(i, 1).replace(pattern, '');
    }
    sundryUtil.searchUniItemsFromRedis(pageNum,pageSize,keywordRs,typeLs,vips,orderBy,withTotalPage,function (err, data) {
        if(err) {
            return retUtil.ret_err_json(err, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.post('/searchDesignAndPrint_app_v6_6', searchDesignAndPrint_app_v6_6);
router.get('/searchDesignAndPrint_app_v6_6', searchDesignAndPrint_app_v6_6);



/***
 * 获取弹框信息_APP_6_6 添加
 * @author 李强
 * @date 2018-5-15
 * 方式: GET
 * 参数: device     设备的标识  610
 * 参数: appVer     版本号
 *测试地址 : http://localhost:3000/yjsc/v2/sundry/getActivityWindowInfo_app_v6_6?
 */
function getActivityWindowInfo_app_v6_6(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');

    if (!device || !appVer ) {
        return retUtil.ret_err_json("参数有误", res);
    }
    // 优先使用缓存
    var uri = "yjsc/v2/sundry/getActivityWindowInfo_app_v6_6";
    var para = {
        device : device , // 每种来源记录一份缓存
        appVer : appVer
    };
    StaticizeUtil.getByRedis(__filename, uri, para, function(error, content) {
        if (content && CONFIG.DEBUG !== 1) { // 有缓存, 且不是本地调试, 直接返回
            return retUtil.ret_succ_json(content, res);
        }
        sundryUtil.getActivityWindowInfo_app_v6_6(function (err, data) {
            if (err) {
                return retUtil.ret_err_json(err, res);
            }
            var content = StaticizeUtil.recordByRedis(__filename, uri, para, data);  // 缓存
            return retUtil.ret_succ_json(content, res);
        });
    });
}
router.post('/getActivityWindowInfo_app_v6_6', getActivityWindowInfo_app_v6_6);
router.get('/getActivityWindowInfo_app_v6_6', getActivityWindowInfo_app_v6_6);

/*
* 获取字体列表
*
* */

function getFontList(req, res, next) {
    sundryUtil.getFontList(function (err, data) {
        if (err) {
            return retUtil.ret_err_json(err, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.post('/getFontList', getFontList);
router.get('/getFontList', getFontList);





//==========================================================================
module.exports = router;